class Solution
{
public:
    vector<vector<int>> levelOrder(TreeNode* root)
    {
        if (!root) return {};

        vector<vector<int>> res;
        queue<TreeNode*> q;
        q.push(root);

        while (!q.empty())
        {
            int size = q.size();
            vector<int> level;

            for (int i = 0; i < size; ++i)
            {
                TreeNode* t = q.front();
                q.pop();
                level.push_back(t->val);

                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }

            res.push_back(level);
        }

        return res;
    }
};